define(['app'], function(app) {
    'use strict';

    var controller = function controller($scope, Starbusiness_publicity, GoPage, $timeout, GoDialog) {
        $scope.input = new Object();
        //滑动切换
        $scope.selectTab = 0;
        $scope.activeTab = function(index) {
            $scope.selectTab = index;
            $ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
        };
        //滑动切换
        $scope.slideChanged = function(index) {
            $scope.selectTab = index;
        };

        //跳转到视频编辑页面
        $scope.onshow_edit = function() {
            GoPage.go_states("starbusiness_publicity_apply");
        }
        $scope.onshow_spushinfo = function() {
            GoPage.go_states("starbusiness_pushinfo");
        }

        //加载数据
        $scope.input.status = 0;
        $scope.init_data = function(val) {
            $scope.input.status = val;
            Starbusiness_publicity.getvideolist($scope.input.status, 1, function(data) {
                $scope.vediolist = data;
            });
        }
        $scope.init_data(1); //必须在定义之后

        //跳转到视频编辑页面
        $scope.onshow_publicity_apply = function(id) {
            GoPage.go_states("starbusiness_publicity_apply", {
                "id": id
            });
        }
        $scope.showVedio = function(id){
        	GoPage.go_states("stardetail",{"detail_id":id});
        }

        //下拉刷新
        $scope.input.nPage = 1;
        $scope.isLoad = false; //可以加载
        //下拉刷新 判断哪个模块进行刷新
        $scope.doRefresh = function() {
            //防止重复加载
            $scope.isLoad = true;
            Starbusiness_publicity.getvideolist($scope.input.status, 1, function(data) {
                    if(data != null) {
                        $scope.vediolist = data;
                    } else{
                    	$scope.vediolist = new Object();
                    }
                    $scope.input.nPage = 1;
                },
                function() {
                    $scope.$broadcast('scroll.refreshComplete');
                    $timeout(function() {
                        $scope.isLoad = false;
                    }, 1500);
                });
        };
        //加载更多
        $scope.doRefreshMore = function() {
            //防止重复加载
            $scope.isLoad = true;
            Starbusiness_publicity.getvideolist($scope.input.status, $scope.input.nPage + 1, function(data) {
                    if(data != null) {
                        if($scope.vediolist.advertis != undefined) {
                            $scope.vediolist.advertis = $scope.vediolist.advertis.concat(data.advertis);
                            $scope.input.nPage += 1;
                        } else {
                            $$scope.vediolist = data;
                        }
                    }
                },
                function() {
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                    $timeout(function() {
                        $scope.isLoad = false;
                    }, 1500);

                });
        };

        //删除宣传片
        $scope.delete = function(id, index) {
            Starbusiness_publicity.del_video(id, function(data) {
                if(data.success) {
                    GoDialog.toast("删除成功！");
                    $scope.vediolist.advertis.splice(index, 1);
                } else {
                    GoDialog.toast(data.msg);
                }
            });
        }


    }

    controller.$inject = ['$scope', 'Starbusiness_publicity', 'GoPage', '$timeout', "GoDialog"];

    return app.registerController("starbusiness_publicityctrl",controller);
});